#include <iostream>
#include <vector>
#include <string>
// 导入 deviceManager 头文件
#include "deviceManager.h"
// 导入 relationalStore 头文件
#include "relationalStore.h"

int main() {
    // 定义 STORE_CONFIG
    relationalStore::StoreConfig STORE_CONFIG;
    STORE_CONFIG.name = "RdbTest.db";
    STORE_CONFIG.securityLevel = relationalStore::SecurityLevel::S1;

    // 调用 getRdbStore 函数
    relationalStore::getRdbStore(this->context, STORE_CONFIG, [](relationalStore::Error err, relationalStore::RdbStore store) {
        // 创建 EMPLOYEE 表
        store.executeSql("CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)", nullptr, [](relationalStore::Error err) {
            // 设置分布式同步表
            store.setDistributedTables({"EMPLOYEE"});
            // 进行数据的相关操作
        });
    });

    // 构造用于同步分布式表的谓词对象
    relationalStore::RdbPredicates predicates("EMPLOYEE");

    // 调用 sync 函数
    store.sync(relationalStore::SyncMode::SYNC_MODE_PUSH, predicates, [](relationalStore::Error err, std::vector<std::vector<std::string>> result) {
        // 判断数据同步是否成功
        if (err) {
            std::cout << "Failed to sync data. Code:" << err.code << ", message:" << err.message << std::endl;
            return;
        }
        std::cout << "Succeeded in syncing data." << std::endl;
        for (int i = 0; i < result.size(); i++) {
            std::cout << "device:" << result[i][0] << ", status:" << result[i][1] << std::endl;
        }
    });

    // 定义 storeObserver 函数
    auto storeObserver = [](std::vector<std::string> devices) {
        for (int i = 0; i < devices.size(); i++) {
            std::cout << "The data of device:" << devices[i] << " has been changed." << std::endl;
        }
    };

    try {
        // 调用 on 函数，注册数据库的观察者
        // 当分布式数据库中的数据发生更改时，将调用回调
        store.on("dataChange", relationalStore::SubscribeType::SUBSCRIBE_TYPE_REMOTE, storeObserver);
    } catch (relationalStore::Error err) {
        std::cout << "Failed to register observer. Code:" << err.code << ", message:" << err.message << std::endl;
    }

    // 当前不需要订阅数据变化时，可以将其取消
    try {
        store.off("dataChange", relationalStore::SubscribeType::SUBSCRIBE_TYPE_REMOTE, storeObserver);
    } catch (relationalStore::Error err) {
        std::cout << "Failed to register observer. Code:" << err.code << ", message:" << err.message << std::endl;
    }

    // 调用 createDeviceManager 函数
    deviceManager::createDeviceManager("com.example.appdatamgrverify", [](deviceManager::Error err, deviceManager::DeviceManager manager) {
        if (err) {
            std::cout << "Failed to create device manager. Code:" << err.code << ", message:" << err.message << std::endl;
            return;
        }
        std::vector<deviceManager::DeviceInfo> devices = manager.getTrustedDeviceListSync();
        std::string deviceId = devices[0].deviceId;

        // 构造用于查询分布式表的谓词对象
        relationalStore::RdbPredicates predicates("EMPLOYEE");

        // 调用 remoteQuery 函数，并返回查询结果
        store.remoteQuery(deviceId, "EMPLOYEE", predicates, {"ID", "NAME", "AGE", "SALARY", "CODES"}, [](relationalStore::Error err, relationalStore::ResultSet resultSet) {
            if (err) {
                std::cout << "Failed to remoteQuery data. Code:" << err.code << ", message:" << err.message << std::endl;
                return;
            }
            std::cout << "ResultSet column names: " << resultSet.columnNames << ", column count: " << resultSet.columnCount << std::endl;
        });
    });

    return 0;
}
